<template>
  <div class="merchant-home">
    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon today-orders">
                <el-icon><Document /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ todayStats.orderCount }}</div>
                <div class="stat-label">今日订单</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon today-sales">
                <el-icon><Money /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">¥{{ todayStats.totalSales }}</div>
                <div class="stat-label">今日销售额</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon pending-orders">
                <el-icon><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ pendingOrdersCount }}</div>
                <div class="stat-label">待处理订单</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="6">
          <el-card class="stat-card">
            <div class="stat-content">
              <div class="stat-icon menu-items">
                <el-icon><Food /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-value">{{ menuItemsCount }}</div>
                <div class="stat-label">菜品数量</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    
    <div class="content-section">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-card class="recent-orders">
            <template #header>
              <div class="card-header">
                <span>最近订单</span>
                <el-button text @click="$router.push('/merchant/orders')">
                  查看全部
                </el-button>
              </div>
            </template>
            
            <el-table :data="recentOrders" style="width: 100%">
              <el-table-column prop="orderNumber" label="订单号" width="120" />
              <el-table-column prop="customerName" label="客户" width="100" />
              <el-table-column prop="totalAmount" label="金额" width="100">
                <template #default="scope">
                  ¥{{ scope.row.totalAmount }}
                </template>
              </el-table-column>
              <el-table-column prop="status" label="状态" width="100">
                <template #default="scope">
                  <el-tag :type="getOrderStatusType(scope.row.status)">
                    {{ getOrderStatusText(scope.row.status) }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="createdAt" label="下单时间" width="180">
                <template #default="scope">
                  {{ formatTime(scope.row.createdAt) }}
                </template>
              </el-table-column>
              <el-table-column label="操作" width="120">
                <template #default="scope">
                  <el-button
                    type="primary"
                    size="small"
                    @click="viewOrder(scope.row.id)"
                  >
                    查看详情
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
        
        <el-col :span="8">
          <el-card class="quick-actions">
            <template #header>
              <span>快捷操作</span>
            </template>
            
            <div class="action-buttons">
              <el-button
                type="primary"
                size="large"
                style="width: 100%; margin-bottom: 15px"
                @click="$router.push('/merchant/menu')"
              >
                <el-icon><Plus /></el-icon>
                添加菜品
              </el-button>
              
              <el-button
                type="success"
                size="large"
                style="width: 100%; margin-bottom: 15px"
                @click="$router.push('/merchant/orders')"
              >
                <el-icon><Document /></el-icon>
                查看订单
              </el-button>
              
              <el-button
                type="warning"
                size="large"
                style="width: 100%; margin-bottom: 15px"
                @click="$router.push('/merchant/statistics')"
              >
                <el-icon><TrendCharts /></el-icon>
                数据统计
              </el-button>
              
              <el-button
                type="info"
                size="large"
                style="width: 100%"
                @click="$router.push('/merchant/profile')"
              >
                <el-icon><Setting /></el-icon>
                店铺设置
              </el-button>
            </div>
          </el-card>
          
          <el-card class="business-status" style="margin-top: 20px">
            <template #header>
              <span>营业状态</span>
            </template>
            
            <div class="status-info">
              <div class="status-item">
                <span class="label">当前状态：</span>
                <el-tag :type="merchantInfo?.status === 'OPEN' ? 'success' : 'danger'">
                  {{ getStatusText(merchantInfo?.status) }}
                </el-tag>
              </div>
              
              <div class="status-item">
                <span class="label">营业时间：</span>
                <span>{{ merchantInfo?.businessHours || '未设置' }}</span>
              </div>
              
              <div class="status-item">
                <span class="label">联系电话：</span>
                <span>{{ merchantInfo?.phone || '未设置' }}</span>
              </div>
              
              <div class="status-item">
                <span class="label">店铺地址：</span>
                <span>{{ merchantInfo?.address || '未设置' }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Document, Money, Clock, Food, Plus, Setting, TrendCharts } from '@element-plus/icons-vue'
import { getMyMerchant } from '../../api/merchant'
import { getRecentOrders, getTodayStats } from '../../api/order'
import { getMenuItemsCount } from '../../api/menu'
import dayjs from 'dayjs'

const router = useRouter()

const merchantInfo = ref(null)
const recentOrders = ref([])
const todayStats = ref({
  orderCount: 0,
  totalSales: 0
})
const pendingOrdersCount = ref(0)
const menuItemsCount = ref(0)

const getOrderStatusText = (status) => {
  const statusMap = {
    'PENDING': '待确认',
    'CONFIRMED': '已确认',
    'PREPARING': '制作中',
    'READY': '待配送',
    'DELIVERING': '配送中',
    'COMPLETED': '已完成',
    'CANCELLED': '已取消'
  }
  return statusMap[status] || status
}

const getOrderStatusType = (status) => {
  const typeMap = {
    'PENDING': 'warning',
    'CONFIRMED': 'primary',
    'PREPARING': 'info',
    'READY': 'success',
    'DELIVERING': 'success',
    'COMPLETED': 'success',
    'CANCELLED': 'danger'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    'OPEN': '营业中',
    'CLOSED': '已打烊',
    'PENDING': '审核中',
    'REJECTED': '已拒绝'
  }
  return statusMap[status] || status
}

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm')
}

const viewOrder = (orderId) => {
  router.push(`/merchant/orders/${orderId}`)
}

const loadData = async () => {
  try {
    // 加载商家信息
    const merchantResponse = await getMyMerchant()
    merchantInfo.value = merchantResponse.data
    
    // 加载今日统计
    const statsResponse = await getTodayStats()
    todayStats.value = statsResponse.data
    
    // 加载最近订单
    const ordersResponse = await getRecentOrders()
    recentOrders.value = ordersResponse.data
    
    // 计算待处理订单数量
    pendingOrdersCount.value = recentOrders.value.filter(
      order => ['PENDING', 'CONFIRMED', 'PREPARING'].includes(order.status)
    ).length
    
    // 加载菜品数量
    const menuResponse = await getMenuItemsCount()
    menuItemsCount.value = menuResponse.data
  } catch (error) {
    ElMessage.error('加载数据失败')
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.merchant-home {
  padding: 20px;
}

.stats-section {
  margin-bottom: 30px;
}

.stat-card {
  height: 120px;
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  font-size: 24px;
  color: white;
}

.today-orders {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.today-sales {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.pending-orders {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.menu-items {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #7f8c8d;
}

.content-section {
  margin-top: 30px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-buttons {
  display: flex;
  flex-direction: column;
}

.status-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.label {
  color: #7f8c8d;
  font-size: 14px;
}
</style>
